package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import model.SearchPhotoModel;
import model.SearchResultModel;
import model.UserPermissions;
import model.ViewPhotoModel;
import view.ViewDailyPhotosGUI;
import view.ViewPhotoGUI;
import client.CliMessage;
import client.IObserve;

/**
 * The Class ViewDailyPhotosController.
 * @author Dmitry
 */
public class ViewDailyPhotosController extends AbstractController implements IObserve {
	
	/** The view daily photo view. */
	private ViewDailyPhotosGUI viewDailyPhotoView ;
	
	/** The view daily photo model. */
	private SearchResultModel viewDailyPhotoModel;
	
	/** user permissions for the query. */
	UserPermissions up = UserPermissions.getInstance(0, null, null);
	
	/**
	 * Instantiates a new view daily photos controller.
	 *
	 * @param theView - ViewDailyPhotoGUI which displays the search spinners.
	 * @param theModel - ViewDailyPhotoModel which holds the images that has been returned from the data base.
	 */
	public ViewDailyPhotosController(ViewDailyPhotosGUI theView, SearchResultModel theModel) {
		super(theView, theModel);
		this.viewDailyPhotoView = theView;
		this.viewDailyPhotoModel = theModel;
		viewDailyPhotoView.addStartButtonListener(new StartButtonListener());
	}

	/* (non-Javadoc)
	 * @see client.IObserve#handleDBRequestSuccess()
	 */
	@Override
	public void handleDBRequestSuccess() {
		// TODO Auto-generated method stub
		
	}

	/* (non-Javadoc)
	 * @see client.IObserve#handleNullSearchResult()
	 */
	@Override
	public void handleNullSearchResult() {
		System.out.println("inform user: no such photo!");
		viewDailyPhotoView.displayInfoMessage("There are no photos matching search information."
										,"Photos Not Found", 1);
		
	}

	/* (non-Javadoc)
	 * @see client.IObserve#handleDBSearchResult(client.CliMessage)
	 */
	@Override
	public void handleDBSearchResult(CliMessage dbResult) {
		System.out.println("This is the result!!");
		ArrayList<SearchPhotoModel> imageDetailsList = new ArrayList<SearchPhotoModel>();
		srs = dbResult.getSrs();
		String timeStamp;
		int location;
		String linkToFile;
		
		for (int i = 0; i < srs.size(); i++) {
			timeStamp = srs.get(i).get("TIMESTAMP");
			location = Integer.parseInt(srs.get(i).get("LOCATION"));
			linkToFile = srs.get(i).get("LINKTOFILE");
			SearchPhotoModel imgToSave = new SearchPhotoModel(timeStamp,location,linkToFile);
			imageDetailsList.add(imgToSave);
		}
		viewDailyPhotoModel = new SearchResultModel(imageDetailsList);
		ViewPhotoGUI viewPhotoView = new ViewPhotoGUI();
		ViewPhotoModel viewPhotoModel = new ViewPhotoModel(viewDailyPhotoModel);
		ViewPhotoController viewPhotoCont = new ViewPhotoController(viewPhotoView, viewPhotoModel);
		setCurrentObserver(viewPhotoCont);
		setBackListener(this, viewPhotoView);
		
	}
	
	/**
	 * The listener interface for receiving startButton events.
	 * It fires an event if the OK button was clicked, creates a query and searches data base for the wanted 
	 * images.
	 *@see StartButtonEvent
	 */
	class StartButtonListener implements ActionListener{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			String sql = "searchSELECT p.Location,p.TimeStamp,p.LinkToFile " 
							    +"FROM customer_photos AS cp, photos AS p "
							    +"WHERE cp.uid = "+ up.getCurrUserID() + " "
							    +"AND cp.TimeStamp BETWEEN  '" + viewDailyPhotoView.getFromTimestamp() + "' AND '"+viewDailyPhotoView.getToTimestamp()+"' "
							    +"AND cp.TimeStamp = p.TimeStamp "
							    +"AND cp.location = ANY (SELECT ca.balance "
							    						 +"FROM customer_account AS ca "
							    						 +"WHERE ca.uid = "+up.getCurrUserID()+")";
			sendToServer(sql);
		}
		
	}
}
